<template>
  <el-dialog
    :visible.sync="visible"
    :title="!dataForm.id ? $t('add') : $t('update')"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form
      :model="dataForm"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '100px'"
    >
      <el-row>
        <!-- <el-col :span="12">
        <el-form-item label="房源id" prop="houseId">
            <el-input v-model="dataForm.houseId" placeholder="房源id"></el-input>
        </el-form-item>
        </el-col>-->
        <el-col :span="12">
          <el-form-item label="部门">
            <el-cascader
              style="width:100%"
              v-model="deptSelectVal"
              :options="this.$store.state.DeptList"
              :show-all-levels="false"
              @change="deptIdChange"
              :props="deptProps"
              ref="myCascader"
              clearable
            ></el-cascader>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收钥人">
            <el-select
              v-model="dataForm.userId"
              placeholder="请选择"
              style="width:100%"
              clearable
              :disabled="dataForm.deptId==''"
            >
              <el-option
                v-for="item in maintainList"
                :key="item.id"
                :label="item.realName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收钥时间" prop="keyDate">
            <!-- <el-input v-model="dataForm.keyDate" placeholder="收钥时间"></el-input> -->
            <el-date-picker
              v-model="dataForm.keyDate"
              type="date"
              placeholder="收钥时间"
              value-format="timestamp"
              style="width:100%"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="钥匙编号" prop="number">
            <el-input v-model="dataForm.number" placeholder="钥匙编号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- <el-form-item label="是否有门禁" prop="accessControl">
            <el-input v-model="dataForm.accessControl" placeholder="是否有门禁"></el-input>
          </el-form-item>-->
          <el-form-item label="门禁" prop="accessControl">
            <el-select v-model="dataForm.accessControl" style="width:100%" placeholder="是否有门禁">
              <el-option label="是" value="1"></el-option>
              <el-option label="否" value="0"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="门禁密码" prop="password">
            <el-input v-model="dataForm.password" placeholder="门禁密码"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="钥匙位置" prop="address">
            <el-input v-model="dataForm.address" placeholder="钥匙位置"></el-input>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
                    <el-form-item label="修改人" prop="updater">
                        <el-input v-model="dataForm.updater" placeholder="修改人"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="修改时间" prop="updateDate">
                        <el-input v-model="dataForm.updateDate" placeholder="修改时间"></el-input>
                    </el-form-item>
        </el-col>-->
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="dataForm.remark" placeholder type="textarea" :rows="2"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: "",
        houseId: this.$route.query.ID ? this.$route.query.ID : "",
        userId: "",
        deptId: "",
        keyDate: "",
        number: "",
        address: "",
        accessControl: "",
        password: "",
        remark: "",
        creator: "",
        createDate: "",
        updater: "",
        updateDate: ""
      },
      deptSelectVal: "",
      deptProps: {
        value: "id",
        label: "name",
        checkStrictly: true
      },
      maintainList: []
    };
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/house/housekey/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data
          };
        })
        .catch(() => {});
    },
    //部门change
    deptIdChange(val) {
      let a = "";
      if (this.$refs.myCascader.getCheckedNodes()[0]) {
        a = this.$refs.myCascader.getCheckedNodes()[0].value;
        this.dataForm.deptId = a;
        this.getUserList(a);
      } else {
        this.dataForm.deptId = "";
      }
    },
    // 获取部门员工列表
    getUserList(deptId) {
      let _this = this;
      this.$http
        .get(`/sys/user/deptUserList`, {
          params: {
            deptId: deptId
          }
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          _this.maintainList = res.data;
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function() {
        this.$refs["dataForm"].validate(valid => {
          if (!valid) {
            return false;
          }
          this.$http[!this.dataForm.id ? "post" : "put"](
            "/house/housekey/",
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    )
  }
};
</script>
